/*
 * Copyright and license 
 */
package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * @author JuanK
 *
 */
public class PanelBefore extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String imageFilepath;
	private ImageIcon imageAfter;
	private JLabel labelImage;
	private int maxWidth;
	private int maxHeight;
	private final int spacing = 50;
	
	public PanelBefore()
	{
		init();
	}
	/**
	 * Change the image in the GUI
	 * @param filePath
	 */
	public void setImage(String filePath)
	{
		imageFilepath = filePath;
		ImageIcon imageIcon = new ImageIcon(imageFilepath);
		maxWidth = this.getSize().width - spacing;
		maxHeight = this.getSize().height - spacing;
		labelImage.setPreferredSize(new Dimension(maxWidth, maxHeight));
		labelImage.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		int newWidth = imageIcon.getIconWidth();
		int newHeight = imageIcon.getIconHeight();
		
		double scaleWidth = 1;
		double scaleHeight = 1;
		
		if(newHeight > maxHeight || newWidth > maxWidth)
		{
			if(newHeight > maxHeight)
			{
				scaleHeight = (double)maxHeight / (double)newHeight;
				newHeight = maxHeight;
				newWidth = (int)(newWidth * scaleHeight);
				if(newWidth > maxWidth)
				{
					scaleWidth = (double)maxWidth / (double)newWidth;
					newWidth = maxWidth;
					newHeight = (int)(newHeight * scaleWidth);
				}
			}
			else
			{
				scaleWidth = (double)maxWidth / (double)newWidth;
				newWidth = maxWidth;
				newHeight = (int)(newHeight * scaleWidth);
				if(newHeight > maxHeight)
				{
					scaleHeight = (double)maxHeight / (double)newHeight;
					newHeight = maxHeight;
					newWidth = (int)(newWidth * scaleHeight);
				}
			}
		}
		
		Image image = imageIcon.getImage();
		BufferedImage buffer = new BufferedImage(maxWidth, maxHeight, BufferedImage.TYPE_INT_ARGB);
		Graphics graphics = buffer.getGraphics();
		graphics.drawImage(image, (maxWidth - newWidth) / 2, (maxHeight - newHeight) / 2, newWidth, newHeight, null);
		imageIcon = new ImageIcon(buffer);
		imageIcon.getImage().flush();
		labelImage.setIcon(imageIcon); 
		this.repaint();
	}
	
	/**
	 * @return the imageFilepath
	 */
	public String getImageFilepath() {
		return imageFilepath;
	}
	
	public BufferedImage getBufferedImage()
	{
		ImageIcon imageIcon = new ImageIcon(imageFilepath);
		BufferedImage buffer = new BufferedImage(imageIcon.getIconWidth(), imageIcon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
		Graphics graphics = buffer.getGraphics();
		graphics.drawImage(imageIcon.getImage(), 0, 0, imageIcon.getIconWidth(), imageIcon.getIconHeight(), null);
		return buffer;
	}

	public String getOutputImageFilepath()
	{
		return "."+File.separatorChar+"Files"+File.separatorChar+"~" + imageFilepath.substring(imageFilepath.lastIndexOf(File.separatorChar) + 1);
	}
	private void init()
	{
		labelImage = new JLabel(imageAfter);
		this.add(labelImage);
	}
	public void setWidthAndHeight(int width, int height)
	{
		maxWidth = width;
		maxHeight = height;
	}

}
